package netty.fry;

import net.dreamlu.iot.mqtt.core.client.MqttClient;

import java.nio.ByteBuffer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @Author zhangtao
 * @create 2021/12/27 13:36
 */
public class JixiaoFry {
    public static void main(String[] args) {
        String COOK_START = "{\n" +
                "  \"GUID\":\"2112271001003086\",\n" +
                "  \"SOURCE\":\"a4:e5:7c:55:ef:74\",\n" +
                "  \"CORRELATION_ID\":\"2112271001003086\",\n" +
                "  \"DATE_TIME\":\"2021-12-27T10:33:10+0000\",\n" +
                "  \"TAG_NAME\":\"COOK START\",\n" +
                "  \"PROD_IDX\":\"20\",\n" +
                "  \"PROD_NAME\":\"PROD 20\",\n" +
                "  \"VAT\":\"RIGHT\",\n" +
                "  \"COOK TIME\":\"210\",\n" +
                "  \"ACT TEMP\":\"3489\",\n" +
                "  \"SET TEMP\":\"3500\"\n" +
                "}";

        String LEFT_ACT_TEMP="{\n" +
                "  \"GUID\":\"2012311709024542\",\n" +
                "  \"SOURCE\":\"a4:e5:7c:55:ef:74\",\n" +
                "  \"CORRELATION_ID\":\"2112271001003086\",\n" +
                "  \"DATE_TIME\":\"2020-12-31T17:09:42+0000\",\n" +
                "  \"TAG_NAME\":\"ACT TEMP\",\n" +
                "  \"VAT\":\"LEFT\",\n" +
                "  \"DATA\":\"3550\",\n" +
                "}";
        String RIGHT_ACT_TEMP="{\n" +
                "  \"GUID\":\"2012311709024542\",\n" +
                "  \"SOURCE\":\"a4:e5:7c:55:ef:74\",\n" +
                "  \"CORRELATION_ID\":\"2112271001003086\",\n" +
                "  \"DATE_TIME\":\"2020-12-31T17:09:42+0000\",\n" +
                "  \"TAG_NAME\":\"ACT TEMP\",\n" +
                "  \"VAT\":\"RIGHT\",\n" +
                "  \"DATA\":\"2800\",\n" +
                "}";
        String QUANTITY = "{\n" +
                "  \"GUID\":\"2112271001013247\",\n" +
                "  \"SOURCE\":\"a4:e5:7c:55:ef:74\",\n" +
                "  \"CORRELATION_ID\":\"2112271001003086\",\n" +
                "  \"DATE_TIME\":\"2021-12-27T10:33:20+0000\",\n" +
                "  \"TAG_NAME\":\"QUANTITY\",\n" +
                "  \"PROD_IDX\":\"20\",\n" +
                "  \"PROD_NAME\":\"PROD 20\",\n" +
                "  \"QUANTITY\":\"2\",\n" +
                "}";

        String COOK_DONE = "{\n" +
                "  \"GUID\":\"2112271004065046\",\n" +
                "  \"SOURCE\":\"a4:e5:7c:55:ef:74\",\n" +
                "  \"CORRELATION_ID\":\"2112271001003086\",\n" +
                "  \"DATE_TIME\":\"2021-12-27T10:36:23+0000\",\n" +
                "  \"TAG_NAME\":\"COOK DONE\",\n" +
                "  \"PROD_IDX\":\"20\",\n" +
                "  \"PROD_NAME\":\"PROD 20\",\n" +
                "  \"VAT\":\"RIGHT\",\n" +
                "  \"COOK TIME\":\"0\",\n" +
                "  \"ACT TEMP\":\"3517\",\n" +
                "  \"SET TEMP\":\"3500\"\n" +
                "}";

        // 初始化 mqtt 客户端
        MqttClient client = MqttClient.create()
                .ip("127.0.0.1")
                .port(1883)
                .connect();

        ScheduledExecutorService schedule = Executors.newScheduledThreadPool(4);
        schedule.scheduleAtFixedRate(
                () -> {
                    try {
                        client.publish("topic:a4:e5:7c:55:ef:74-PUB", ByteBuffer.wrap(COOK_START.getBytes()));
//                        Thread.sleep(30000);
                        client.publish("topic:a4:e5:7c:55:ef:74-PUB", ByteBuffer.wrap(LEFT_ACT_TEMP.getBytes()));
                        client.publish("topic:a4:e5:7c:55:ef:74-PUB", ByteBuffer.wrap(RIGHT_ACT_TEMP.getBytes()));
                        client.publish("topic:a4:e5:7c:55:ef:74-PUB", ByteBuffer.wrap(QUANTITY.getBytes()));
//                        Thread.sleep(30000);
                        client.publish("topic:a4:e5:7c:55:ef:74-PUB", ByteBuffer.wrap(COOK_DONE.getBytes()));
                        Thread.sleep(30000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }, 5, 60 * 60, TimeUnit.SECONDS);
    }
}
